﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSAVA02
    { 
   
        public TSAVA02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VVAKOLL();
            DataContext = MV; 
            InitializeComponent();   
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwVAK = dwMaster;
            if (dwMaster == null)
            {
                KEY = "";
            }
            else
            {
                KEY = dwMaster["VAKKOLL"].ToString();
            }
        }
     
        public override void LOADALL()
        {
            dwData = TBMain.NewRow();
            dwData["VAKMANDANT"] = app.Mandant;
            dwData["VAKKM1"] = KM1.PadRight(5);
            SResult = TMV.QVAK_1(dwData);
            MergeData(SResult,false);
            Locate("VAKKOLL", KEY);
        }
        public override void OnReSetChg()
        {
            if (!IsLoaded)
                return;
            if (!IsReset)
                return;
            LOCATEACTIVEKOLL("VAKSPERRDAT", "0");
            //base.OnReSetChg();
        }
        public   void LOCATEACTIVEKOLL(string key, string value)
        {
            if (!IsLoaded)
                return;
            if (vwMaster.View == null)
                return;
            vwMaster.View.MoveCurrentTo(-1);
            if (value == "")
                return;
            if (!TBMain.Columns.Contains(key))
                return;
            //string cfilter = cvMaster.CustomFilter;
            //cvMaster.CustomFilter = null;
            //System.Collections.IEnumerable src = vwMaster.View.SourceCollection;
            string cfilter = ((BindingListCollectionView)vwMaster.View).CustomFilter;
            ((BindingListCollectionView)vwMaster.View).CustomFilter = null;
            filter = "" + key + "=" + value.Trim() + "";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                int idx = TBMain.Rows.IndexOf(row);
                vwMaster.View.MoveCurrentToPosition(idx);
                break;
            }
         ((BindingListCollectionView)vwMaster.View).CustomFilter = cfilter;
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded) return;
            base.Window_Loaded(sender, e);
            LOADALL();
        }
        public override void LOADBYAUF()
        {
            Locate("VAKKOLL",ORDERNUM);
            base.LOADBYAUF();
        }
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                LOADALL();
            }
            else
            {
                VICC_WFEVENTS.SVWF.VA02(RUNID); 
            }
            base.ManageCommand(sender, e);
        }
        public override void OnAppModeChg()
        {
            base.OnAppModeChg();
            LOADALL();
        } 
    }
}
